package com.lq.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lq.common.api.dto.ProductDTO;
import com.lq.common.result.PageResult;
import com.lq.common.result.Result;
import com.lq.product.entity.Product;
import com.lq.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/products")
@Api(tags = "商品管理", description = "商品相关接口")
public class ProductController {

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "创建商品", notes = "创建一个新的商品")
    @PostMapping("/")
    public Result createProduct(@ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        productService.save(product);
        return Result.success(product);
    }

    @ApiOperation(value = "根据ID获取商品", notes = "根据商品ID获取商品详细信息")
    @GetMapping("/{id}")
    public Result<ProductDTO> getProductById(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        Product product = productService.getById(id);
        ProductDTO productDTO = BeanUtil.copyProperties(product, ProductDTO.class);
        return Result.success(productDTO);
    }

    @ApiOperation(value = "获取所有商品", notes = "获取系统中所有的商品")
    @GetMapping("/")
    public PageResult getAllProducts(
        @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
        @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {
        Page<Product> productPage = new Page<>(page, size);
        Page<Product> result = productService.listProductsByPage(productPage);
        return PageResult.success(result.getRecords(), productPage.getCurrent(), productPage.getSize(), result.getTotal());
    }

    @ApiOperation(value = "根据分类ID获取商品", notes = "根据分类ID获取商品列表")
    @GetMapping("/category/{categoryId}")
    public Result getProductsByCategoryId(@ApiParam(value = "分类ID", required = true) @PathVariable Long categoryId) {
        List<Product> products = productService.getByCategoryId(categoryId);
        return Result.success(products);
    }

    @ApiOperation(value = "更新商品", notes = "根据商品ID更新商品信息")
    @PutMapping("/{id}")
    public Result updateProduct(@ApiParam(value = "商品ID", required = true) @PathVariable Long id,
                                @ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        product.setId(id);
        productService.updateById(product);
        return Result.success(product);
    }

    @ApiOperation(value = "删除商品", notes = "根据商品ID删除商品")
    @DeleteMapping("/{id}")
    public Result deleteProduct(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        productService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "更新商品库存", notes = "根据商品ID更新库存数量")
    @PutMapping("/{id}/stock")
    public Result updateStock(@ApiParam(value = "商品ID", required = true) @PathVariable Long id,
                              @ApiParam(value = "库存数量", required = true) @RequestParam Integer quantity) {
        boolean result = productService.updateStock(id, quantity);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("更新库存失败");
        }
    }

    @ApiOperation(value = "上架商品", notes = "根据商品ID上架商品")
    @PutMapping("/{id}/on-shelf")
    public Result putOnShelf(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        boolean result = productService.putOnShelf(id);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("上架失败");
        }
    }

    @ApiOperation(value = "下架商品", notes = "根据商品ID下架商品")
    @PutMapping("/{id}/off-shelf")
    public Result putOffShelf(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        boolean result = productService.putOffShelf(id);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("下架失败");
        }
    }
}